﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NFL.Business;
using System.Data;

namespace NFL.UI
{
    public partial class testharness : System.Web.UI.Page
    {

        //private List<int> _winners;
        private List<WinnersLosers> _winnersAndLosers;

        private NFL.Business.Business bl;

        public int WeekID { get; set; }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.MaintainScrollPositionOnPostBack = true;

            bl = new Business.Business();
            InitForm();
        }

        protected void InitForm()
        {
            int weekID = 0;
            int division = 1;

            //if (ViewState["division"] != null)
            //{
            //    division = Convert.ToInt32(ViewState["division"].ToString());    
            //}
            //else
            //{
                division = Convert.ToInt32(ddlDivision.SelectedValue.ToString());    
            //}
            //ViewState["division"] = division;

            // Get week id
            weekID = bl.GetWeekID(DateTime.Now);
            this.WeekID = weekID;
            
            // Get pick and scores data
            var pickTable = bl.GetScores(weekID, division);

            // Get the winning teams
            //_winners = bl.GetWinners().ToList();
            _winnersAndLosers = bl.GetWinnersAndLosers(WeekID).ToList();

            gvScores.DataSource = pickTable;
            gvScores.RowStyle.Wrap = false;
            gvScores.CellPadding = 0;
            gvScores.RowStyle.HorizontalAlign = HorizontalAlign.Center;
            gvScores.HeaderStyle.Font.Size = FontUnit.Small;

            gvScores.DataBind();

            // Replace \n with <br /> in header row
            if (gvScores.Rows.Count > 0)
            {
                int columnCount = gvScores.Rows[0].Cells.Count;
                for (int i = 0; i < columnCount; i++)
                {
                    gvScores.Rows[0].Cells[i].Text = gvScores.Rows[0].Cells[i].Text.Replace("\n", "<br />");
                    gvScores.Rows[0].Cells[i].Font.Size = FontUnit.XSmall;
                }

                // Add winner indicator
                gvScores.Rows[0].Cells[0].Text = "* = winner(s)";
            }

        }


        protected void gvScores_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridViewRow row = e.Row;

                if (gvScores.Rows.Count > 0)
                {
                    for (int i = 3; i < gvScores.Rows[0].Cells.Count; i++)
                        {
                            if (!string.IsNullOrEmpty(row.Cells[i].Text.Trim()))
                            {
                                // get if team is a winner
                                if (row.Cells[i].Text.All(char.IsLetter))
                                {
                                    //TODO:  can't have it be this way, must check for both
                                    //row.Cells[i].BackColor = bl.IsTeamWinner(row.Cells[i].Text, _winners) ? System.Drawing.Color.LightGreen : System.Drawing.Color.Red;    

                                    //bool? isWinner = bl.IsTeamWinner(row.Cells[i].Text, WeekID);
                                    //if (isWinner.HasValue)
                                    //{
                                    //    row.Cells[i].BackColor = isWinner.Value ? System.Drawing.Color.LightGreen : System.Drawing.Color.Red;
                                    //}

                                    bool? isWinner = null;
                                    if (_winnersAndLosers.Where(w => w.WinningTeam == bl.GetTeamID(row.Cells[i].Text)).Count() > 0)
                                    {
                                        isWinner = true;
                                    }
                                    if (!isWinner.HasValue && _winnersAndLosers.Where(w => w.LosingTeam == bl.GetTeamID(row.Cells[i].Text)).Count() > 0)
                                    {
                                        isWinner = false;
                                    }

                                    if (isWinner.HasValue)
                                    {
                                        row.Cells[i].BackColor = isWinner.Value ? System.Drawing.Color.LightGreen : System.Drawing.Color.Red;    
                                    }

                                    
                                }
                                
                                //if (row.Cells[i].BackColor == System.Drawing.Color.Red)
                                //{
                                //    row.Cells[i].Style.Add("Style", "background: url(../images/ximage.png)");
                                //    row.Cells[i].Attributes.Add("Style", "background: url(../images/ximage.png)");
 
                                //}
                            }

                        }
                    
                }
 
            }
        }

        protected void ddlDivision_SelectedIndexChanged(object sender, EventArgs e)
        {
            //InitForm();
            //int division = Convert.ToInt32(ddlDivision.SelectedValue.ToString());
            //ViewState["division"] = division;
        }



    }
}